package com.ht.medicaltreatment.viewmodel

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.ht.medicaltreatment.bean.BoxSubmitBean
import com.ht.medicaltreatment.bean.PDScanDataBean
import com.ht.medicaltreatment.bean.TransportRfidSampleBox
import com.xgst.lib.http.ApiResult
import kotlinx.coroutines.launch

class PDScanViewModel: BaseViewModel() {
    fun scanData(uid:String, orderId:String, onResult:(PDScanDataBean)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            val bean = mApi.scanPDData(orderId,uid)
            when(bean){
                is ApiResult.Success -> {
                    onResult(bean.data.data!!)
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    fun submit(boxUID: String, onResult: () -> Unit, onError: (String) -> Unit) {
        viewModelScope.launch {
            val bean = mApi.submitPD("1",boxUID)
            when(bean){
                is ApiResult.Success ->onResult()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    /**
     * 查询uid对应的名字
     */
    fun searchName(uid:MutableList<String>, onResult:(MutableList<String>)->Unit, onError:(String)->Unit){
        viewModelScope.launch {
            when(val bean = mApi.searchNameScan(uid)){
                is ApiResult.Success -> {
                    val sampleInfoList = bean.data.data?.sampleInfoList
                    if(sampleInfoList != null && sampleInfoList.size > 0){
                        val list = arrayListOf<String>()
                        sampleInfoList.forEach {
                            list += it.name
                        }
                        onResult(list)
                    }else{
                        onError("无数据")
                    }
                }
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }

    //替换
    fun replace(oldUID:String,newUID:String,onSuccess:()->Unit,onError: (String) -> Unit){
        viewModelScope.launch {
            when(val bean = mApi.checkGroupReplace(oldUID,newUID)){
                is ApiResult.Success -> onSuccess()
                is ApiResult.Error -> onError(bean.errorMessage)
            }
        }
    }
}
